package cn.bytecollege.controller;

import cn.bytecollege.constant.Resource;
import cn.bytecollege.entity.Page;
import cn.bytecollege.entity.Room;
import cn.bytecollege.service.RoomService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mr.H
 */
@WebServlet(name="RoomListByStateController",value = "/roomListByState")
public class RoomListByStateController extends HttpServlet {
    private RoomService roomService=null;
    public RoomListByStateController(){
        roomService=new RoomService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收参数
        String index=request.getParameter("index");
        String size=request.getParameter("size");
        //用于接收页面中判断查询房间状态的参数
        String flag=request.getParameter("flag");
        //封装参数
        Integer pageIndex=(index==null)?1:Integer.valueOf(index);
        Integer pageSize=(size==null)?10:Integer.valueOf(size);
        Integer stateFlag=Integer.valueOf(flag);

        //封装参数
        Room room=new Room();
        room.setDeleteFlag(0);
        //判断房间状态，如果状态为0查询所有空房，如果为1查询所有已入住的房间信息
        stateFlag=(stateFlag==0)?0:1;
        room.setRoomState(stateFlag);

        //调用service方法
        Page<Room> page=roomService.findByStatePage(pageIndex,pageSize,room);

        //将分页查询得到的结果封装到请求中
        request.setAttribute("page",page);
        //跳转页面
        request.getRequestDispatcher(Resource.PAGE_LOCATION+"/roomList.jsp").forward(request,response);
    }
}
